package com.helloandroid.activitys

import android.app.Activity
import android.content.Intent
import android.graphics.PixelFormat
import android.view.*
import android.widget.Button
import android.widget.TextView
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.tools.AppConfig
import com.helloandroid.tools.MyLog
import java.lang.StringBuilder
import java.util.*

class GMTool (val activity: Activity){

    companion object{
        const val Cmd = "cmd"

        const val Cmd_Resdient = 1 // 常驻通知
        const val Cmd_Hby = 2
        const val Cmd_CanBu = 3
        const val Cmd_BuShu = 4
        const val Cmd_Sign = 5 // 签到
    }

    var gmWindow:View? = null
    var tvMessage:TextView? = null
    var logs = LinkedList<String>()

     fun showWindow(){
        if (gmWindow != null){
            MyLog.elog("gmWindow已存在!")
            return
        }

        gmWindow = LayoutInflater.from(activity).inflate(R.layout.debug_view,null)
        val params = WindowManager.LayoutParams()
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.MATCH_PARENT
        params.x = 0
        params.y = 0
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        params.format = PixelFormat.TRANSPARENT
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        params.gravity = Gravity.LEFT or Gravity.TOP
        activity.windowManager.addView(gmWindow, params)

        val gmView = gmWindow!!

//        var lastX = 0f
//        var lastY = 0f
//        val inWindow = intArrayOf(0,0)
//        val inScreen = intArrayOf(0,0)
//        gmView.setOnTouchListener { v, event ->
//            MyLog.elog("gmView onTouch called!")
//            when(event.action){
//                MotionEvent.ACTION_DOWN->{
//                    lastX = event.rawX
//                    lastY = event.rawY
//                    gmView.getLocationInWindow(inWindow)
//                    gmView.getLocationOnScreen(inScreen)
////                    MyLog.elog("inWindow: ${inWindow[0]},${inWindow[1]}")
////                    MyLog.elog("inScreen: ${inScreen[0]},${inScreen[1]}")
//                }
//                MotionEvent.ACTION_MOVE->{
//                    val dx = event.rawX - lastX
//                    val dy = event.rawY - lastY
//                    lastX = event.rawX
//                    lastY = event.rawY
//                    params.x += dx.toInt()
//                    params.y += dy.toInt()
//                    activity.windowManager.updateViewLayout(gmView,params)
//                }
//            }
//            return@setOnTouchListener false
//        }

         // 关闭按钮
        gmView.findViewById<Button>(R.id.btnClose).setOnClickListener {
            MyLog.elog("close btn clicked!")
            activity.windowManager.removeViewImmediate(gmView)
            gmWindow = null
        }

         initView(gmView)
    }

    fun initView(view:View){
        tvMessage = view.findViewById(R.id.tvMessage)

        findButton(view,R.id.btnResident).setOnClickListener {
            // 发送常驻通知
            sendNotifyRequest(Cmd_Resdient)
        }
        findButton(view,R.id.btnHongbao).setOnClickListener {
            sendNotifyRequest(Cmd_Hby)
        }
        findButton(view,R.id.btnSign).setOnClickListener {
            sendNotifyRequest(Cmd_Sign)
        }
        findButton(view,R.id.btnChiFan).setOnClickListener {
            sendNotifyRequest(Cmd_CanBu)
        }
        findButton(view,R.id.btnBs).setOnClickListener {
            sendNotifyRequest(Cmd_BuShu)
        }
        findButton(view,R.id.btnShowUser).setOnClickListener {
            sendToLog("玩家信息为:${AppUtil.user}")
        }

        findButton(view,R.id.btnKillUi).setOnClickListener {
            android.os.Process.killProcess(android.os.Process.myPid())
        }
    }

    private fun sendNotifyRequest(type:Int){
        // 发送常驻通知
        val intent = Intent()
        intent.action = AppConfig.Action_GmCommond
        intent.putExtra(Cmd,type)
        activity.sendBroadcast(intent)
    }

    private fun findButton(view:View, id:Int):View = view.findViewById(id)

    private fun sendToLog(str:String){
        logs.offer(str)
        if (logs.size > 50){
            logs.poll()
        }
        val sb = StringBuilder()
        val finalText = logs.fold(sb){s,tmpStr->
            s.append(tmpStr).append("\n")
        }.toString()
        tvMessage?.text = finalText
    }

}